<template>
	<view class="group">
		<view class="uni-list" v-if="friendList.length > 0">
			<radio-group @change="radioChange">
				<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in friendList" :key="item.userID" @click="addGroupMember(item)">
					<view>
						<radio :value="String(item.user_id)" :checked="index === current" />
					</view>
					<view class="uni-flex uni-row">
						<view class="group-item-img">
							<img :src="item.face_image ? item.face_image : '/static/img/im/face/face_1.jpg'" alt="">
						</view>
						<view class="group-item-user">
							{{item.realname}}
						</view>
					</view>
				</label>
			</radio-group>
			<!-- <checkbox-group @change="checkboxChange">
				<label class="uni-list-cell uni-list-cell-pd" v-for="item in friendList" :key="item.userID">
					<view>
						<checkbox :value="item.userID" :checked="item.checked" />
					</view>
					<view class="uni-flex uni-row">
						<view class="group-item-img">
							<img :src="item.avatar ? item.avatar : '/static/img/face/face_1.jpg'" alt="">
						</view>
						<view class="group-item-user">
							{{item.userID}}
						</view>
					</view>
				</label>
			</checkbox-group> -->
		</view>
		<view class="uni-list" v-if="friendList.length == 0">暂无IM注册医生</view>
		<!-- <view class="group-add-btn" @click="addGroupMember">
			<button  type="primary" >添加</button>
		</view> -->
	</view>
</template>

<script>
	import {mapState} from "vuex";
	import {setStorage, getStorage} from '@/utils/cookies'
	import tims from '@/commen/tim/tim.js'
	export default {
		data() {
			return {
				friendList:[],current: ""
			}
		},
		computed:{
		
		},
		onLoad(){
			this.getFriendList()
			this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
		},
		methods:{
			//获取好友列表
			async getFriendList(){
				// this.toUserId = JSON.parse(uni.getStorageSync("group")).groupID
				// let _this = this
				// const {data} = await this.$axios({
				//   url: '/api/users/doctor/list'
				// })
				// this.friendList = data.list.data.data
				
				let _this = this
				let promise = this.tim.getFriendList();
				promise.then(function(imResponse) {
				  // console.log(imResponse.data)
				  _this.friendList = imResponse.data
				}).catch(function(imError) {
				  console.warn('getFriendList error:', imError); // 获取好友列表失败的相关信息
				});
			},
			//添加群成员
			addGroupMember(item){
				let _this = this
				let userInfo = {
					SDKAppID: tims.options.SDKAppID,
					user: 'admin',
					userId: 'admin',
				}
				let userSig = tims.genTestUserSig(this.userInfo.userId).userSig
				uni.request({
				    url: `https://console.tim.qq.com/v4/group_open_http_svc/add_group_member?
					sdkappid=${userInfo.SDKAppID}&
					identifier=${userInfo.userId}&
					usersig=${userSig}&
					random=99999999&contenttype=json`,
				    method: 'POST',
				    data: {
						"GroupId": _this.toUserId, // 要操作的群组（必填）
						"MemberList": [ // 一次最多添加300个成员
						{
							"Member_Account": item.userID // 要添加的群成员ID（必填）
						}]
					},
				    success: (res) => {
					   uni.showToast({
					   	icon: 'none',
					   	title: '添加成功',
					   });
					   uni.navigateTo({
					   	url: 'clinicChat'
					   })
				    },
				    fail: (err) => {
				       console.log(err)
				    }
				})
			},
			checkboxChange: function (e) {
				var items = this.friendList,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if(values.indexOf(item.value) >= 0){
						this.$set(item,'checked',true)
					}else{
						this.$set(item,'checked',false)
					}
				}
			},
			radioChange(evt) {
				for (let i = 0; i < this.friendList.length; i++) {
					if (this.friendList[i].userID === evt.detail.value) {
						this.current = i;
						break;
					}
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.group{
		width: 100%;
		&-item-img{
			height: 100rpx;line-height: 100rpx;margin: 0 20rpx;
			img{
				height: 100rpx;width: 100rpx;border-radius: 100rpx;border: 1px solid #f1f0f5;
			}
		}
		&-item-user{
			line-height: 100rpx;
		}
		&-add-btn{
			margin: 10rpx 0;
		}
	}
</style>
